/*--------------------------------------------------------------------------------
	DESCRIPTION: Collapsing harmonized dataset (country-year panel, 
				 country-cohort-year panel, and pooled country averages)

--------------------------------------------------------------------------------*/

/*-------------------------------------------------------------------------------
1)	Creating country-year panel dataset (Figure 7)
-------------------------------------------------------------------------------*/

global trust	trust_govt trust_others

clear
use "$data_dir/clean/harmonized_dataset.dta"

keep if born_country!=0
replace weight = 1 if weight==.
collapse $trust [pw = weight], by(ccode survey_year)

foreach var of global trust {
	rename `var' `var'_wo
	label var `var' "`var' (original survey weights)"
}

sort ccode survey_year

drop if ccode==""

egen panelid = group(ccode)
xtset panelid survey_year

tsfill
carryforward ccode, replace

merge m:1 ccode using "$data_dir/raw/ccode_with_regions.dta"
drop if _merge==2
replace region = "Europe" if ccode=="XKX"
replace subregion = "Southern Europe" if ccode=="XKX"
replace region = "Asia" if ccode=="TWN"
replace subregion = "Eastern Asia" if ccode=="TWN"
drop _merge

rename survey_year year

merge 1:1 ccode year using "$data_dir/raw/mpd2023_clean.dta", keepusing(growth_shock growth_rate)
drop if _merge==2
drop _merge
rename growth_shock growth_shock_mpd

xtset panelid year
keep if year>=2005 & year<=2022

save "$data_dir/clean/country_year_panel.dta", replace

/*-------------------------------------------------------------------------------
2)	Create cohort-average level dataset
-------------------------------------------------------------------------------*/

clear
use "$data_dir/clean/harmonized_dataset.dta"

merge m:1 ccode survey_year yob using "$data_dir/raw/lifeexp_variables_data/growth_rate_mpd_var_exp.dta", nogen keepusing(total_growth_rate_obs wgrowth_rate_1)

keep if born_country!=0 & total_growth_rate_obs>=0.9*(age-1) & survey_year!=. 
gen dummy = 1
bys ccode survey_year cohort: egen total = total(dummy)
replace weight = 1 if weight == .
collapse trust_govt wgrowth_rate_1 total [pw = weight], by(ccode survey_year cohort)
drop if cohort=="" //dropping observations with no year of birth data

rename wgrowth_rate_1 chrt_wgrowth_rate_1
rename total chrt_total

save "$data_dir/clean/country_year_cohort_panel.dta", replace

/*-------------------------------------------------------------------------------
3)	Gather various weighted averages for pooled average dataset
-------------------------------------------------------------------------------*/

clear
use "$data_dir/clean/harmonized_dataset.dta"

keep if born_country!=0

keep if survey_year>=2009 & survey_year<=2019

replace weight = 1 if weight==. //assign full weight so as not to lose sample size in case of missing weights

collapse trust_govt trust_govt_dk_ref trust_others trust_state media_free [pw = weight], by(ccode)

local varlist trust_govt trust_govt_dk_ref trust_others trust_state media_free
foreach var of local varlist {
	rename `var' `var'_wo
	label var `var'_wo "`var' (original survey weights)"
}

save "$data_dir/clean/harmonized_pooled_averages.dta", replace

/*-------------------------------------------------------------------------------
4)	Creating country-year panel with two-year binned treatment (for Appendix G.3)
-------------------------------------------------------------------------------*/

clear
use "$data_dir/clean/country_year_panel.dta"

gen bin = .
replace bin = 1 if year==2005|year==2006
replace bin = 2 if year==2007|year==2008
replace bin = 3 if year==2009|year==2010
replace bin = 4 if year==2011|year==2012
replace bin = 5 if year==2013|year==2014
replace bin = 6 if year==2015|year==2016
replace bin = 7 if year==2017|year==2018
replace bin = 8 if year==2019|year==2020
replace bin = 9 if year==2021|year==2022

bys ccode bin: mipolate growth_shock_mpd bin, groupwise gen(bin_treatment)

replace bin_treatment = 1 if bin_treatment==0.5

collapse trust_govt_wo trust_others_wo bin_treatment, by(ccode bin)

gen bin_str = ""
replace bin_str="2005-2006" if bin == 1 
replace bin_str="2007-2008" if bin == 2 
replace bin_str="2009-2010" if bin == 3 
replace bin_str="2011-2012" if bin == 4 
replace bin_str="2013-2014" if bin == 5 
replace bin_str="2015-2016" if bin == 6 
replace bin_str="2017-2018" if bin == 7 
replace bin_str="2019-2020" if bin == 8
replace bin_str="2021-2022" if bin == 9 

order ccode bin_str bin

egen panelid = group(ccode)

save "$data_dir/clean/country_year_panel_bin_treatment.dta", replace
